/*package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        Greeting("Android")
    }
}*/
package com.example.myapplication

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme
import java.util.Locale

class MainActivity : ComponentActivity() {
    // 存储当前语言状态
    private val currentLanguage = mutableStateOf("zh")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    // 传递当前语言和切换语言的函数到UI组件
                    LanguageScreen(
                        currentLang = currentLanguage.value,
                        onLanguageChange = { lang ->
                            currentLanguage.value = lang
                            // 切换应用语言
                            changeAppLanguage(lang)
                        },
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }

    // 切换应用语言的方法
    private fun changeAppLanguage(langCode: String) {
        val locale = Locale(langCode)
        Locale.setDefault(locale)
        val config = resources.configuration
        config.setLocale(locale)
        resources.updateConfiguration(config, resources.displayMetrics)

        // 重新创建活动以应用语言变化
        recreate()
    }
}

// 主界面组件
@Composable
fun LanguageScreen(
    currentLang: String,
    onLanguageChange: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    // 根据当前语言获取对应的国旗资源ID
    val flagResId = when (currentLang) {

        else -> R.drawable.flag
    }

    // 垂直排列的布局
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(30.dp),
        horizontalAlignment = Alignment.CenterHorizontally, // 水平居中
        verticalArrangement = Arrangement.Top // 垂直方向顶部对齐
    ) {
        // 显示Hello World文本
        Text(
            text = stringResource(id = R.string.hello_world),
            fontSize = 28.sp,
            modifier = Modifier.padding(bottom = 40.dp)
        )

        // 显示国旗图片
        Image(
            painter = painterResource(id = flagResId),
            contentDescription = "Flag",
            modifier = Modifier
                .size(300.dp, 200.dp)
                .padding(bottom = 40.dp)
        )

        // 中文按钮
        Button(
            onClick = { onLanguageChange("zh") },
            modifier = Modifier.padding(bottom = 10.dp)
        ) {
            Text(
                text = stringResource(id = R.string.btn_chinese),
                fontSize = 18.sp
            )
        }

        // 英文按钮
        Button(
            onClick = { onLanguageChange("en") },
            modifier = Modifier.padding(bottom = 10.dp)
        ) {
            Text(
                text = stringResource(id = R.string.btn_english),
                fontSize = 18.sp
            )
        }

        // 日文按钮
        Button(
            onClick = { onLanguageChange("ja") }
        ) {
            Text(
                text = stringResource(id = R.string.btn_japanese),
                fontSize = 18.sp
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        LanguageScreen(currentLang = "zh", onLanguageChange = {})
    }
}

